package nccloud.riart.pubimpl.mob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.relatedApp.RelatedAppVO;
import nc.impl.pubapp.pattern.database.DataAccessUtils;
import nc.vo.pub.BusinessException;
import nc.vo.pub.billtype.BilltypeVO;
import nc.vo.pubapp.pattern.data.IRowSet;
import nccloud.putitf.riart.billtype.IBilltypeService;
import nccloud.riart.constant.BilltypeSenceEnum;
import nccloud.riart.itf.mob.IMobPubService;

public class MobPubServiceImpl implements IMobPubService {

	@Override
	public Map<String, String> getTargetCode(String billtypeOrTranstypecode)
			throws BusinessException {
		Map<String, String> map = new HashMap<String, String>();
		if (billtypeOrTranstypecode != null && !"".equals(billtypeOrTranstypecode)) {
			String pk_group = InvocationInfoProxy.getInstance().getGroupId();
			IBilltypeService bs = (IBilltypeService) NCLocator.getInstance().lookup(IBilltypeService.class);
			boolean isTranstype = bs.isTranstype(billtypeOrTranstypecode,pk_group);
			String realBilltype = billtypeOrTranstypecode;
			if (isTranstype) {
				BilltypeVO bvo = bs.getRealBilltype(billtypeOrTranstypecode,pk_group);
				if (bvo != null) {
					realBilltype = bvo.getPk_billtypecode();
				}
			}
			ArrayList<RelatedAppVO> Approveapp = bs.getRelatedAppByBilltype(
					realBilltype, BilltypeSenceEnum.SENCE_APPROVE.getIntValue());
			if (Approveapp != null && Approveapp.size() > 0) {
				String appcode = ((RelatedAppVO) Approveapp.get(0)).getAppcode();
				String pagecode = ((RelatedAppVO) Approveapp.get(0)).getPagecode();
				map.put("appcode", appcode);
				map.put("pagecode", pagecode);
			}else {
				Approveapp = bs.getRelatedAppByBilltype(
						realBilltype, BilltypeSenceEnum.SENCE_DEFAULT.getIntValue());
				if (Approveapp != null && Approveapp.size() > 0) {
					String appcode = ((RelatedAppVO) Approveapp.get(0)).getAppcode();
					String pagecode = ((RelatedAppVO) Approveapp.get(0)).getPagecode();
					map.put("appcode", appcode);
					map.put("pagecode", pagecode);
				}
			}
			if(map.size()==0) {
				throw new BusinessException("单据类型【"+ realBilltype + "】未关联应用编码，请在单据类型管理处设置！");
			}
			
			//根据页面模板获取表头表体表单区域编码 
			String headareacode = getPageHeadAreaCode(map.get("appcode"),map.get("pagecode"));
			if(headareacode!=null) {
				map.put("headareacode", headareacode);
			}else {
				throw new BusinessException("根据页面模板【"+ map.get("pagecode") + "】获取表头区域编码失败！");
			}
			String bodyareacode = getPageBodyAreaCode(map.get("appcode"),map.get("pagecode"));
			map.put("bodyareacode", bodyareacode);
			
		}
		return map;
	}

	/**
	 * 根据单据类型取到主表名称以及聚合VO的类型名称
	 * 
	 * @param billtype
	 * @return
	 */
	public String[] getMetaInfoByBilltype(String billtype) {

		String strSql = "SELECT a.defaulttablename,a.fullclassname,p.paravalue,a.id FROM"
				+ " md_accessorpara p LEFT JOIN md_class a ON p.id = a.id LEFT JOIN md_component b ON a.componentid = b.id"
				+ " LEFT JOIN bd_billtype c ON b.name = c.component WHERE a.isprimary = 'Y' and c.pk_billtypecode = ";
		strSql = strSql + "'" + billtype + "'";

		DataAccessUtils dao = new DataAccessUtils();
		IRowSet rowset = dao.query(strSql.toString());

		String[][] result = new String[1][3];
		if (rowset != null && rowset.size() > 0) {
			result = rowset.toTwoDimensionStringArray();
		}
		return result[0];
	}
	
	
	/**
	 * 获取页面表头表单区域编码
	 * 按顺序排列，默认取第一个
	 * @param appcode
	 * @param pagecode
	 * @return
	 */
	public String getPageHeadAreaCode(String appcode,String pagecode) {

		String strSql = "select code from pub_area where templetid in (select PK_PAGE_TEMPLET from pub_page_templet where dr=0 and appcode='" + appcode + 
				"' and pagecode = '" + pagecode + "') and dr=0 and areatype='1' and RELATIONCODE is null order by POSITION";

		DataAccessUtils dao = new DataAccessUtils();
		IRowSet rowset = dao.query(strSql.toString());

		String[] result = null;
		if (rowset != null && rowset.size() > 0) {
			result = rowset.toOneDimensionStringArray();
			if(result!=null && result.length>0) {
				return result[0];
			}
		}
		return null;
	}
	
	/**
	 * 获取页面表体表单区域编码
	 * 按顺序排列，默认取第一个
	 * @param appcode
	 * @param pagecode
	 * @return
	 */
	public String getPageBodyAreaCode(String appcode,String pagecode) {

		String strSql = "select code from pub_area where templetid in (select PK_PAGE_TEMPLET from pub_page_templet where dr=0 and appcode='" + appcode + 
				"' and pagecode = '" + pagecode + "') and dr=0 and areatype='2' and RELATIONCODE is null order by POSITION";

		DataAccessUtils dao = new DataAccessUtils();
		IRowSet rowset = dao.query(strSql.toString());

		String[] result = null;
		if (rowset != null && rowset.size() > 0) {
			result = rowset.toOneDimensionStringArray();
			if(result!=null && result.length>0) {
				return result[0];
			}
		}
		return null;
	}
}
